package net.stock.controller;

import com.google.appengine.api.users.UserService;
import net.stock.db.UserAccess;
import net.stock.service.RequestDao;
import net.stock.service.StockService;
import net.stock.utils.StockUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

/**
 * @author naim
 * @date Dec 27, 2009
 */
@SuppressWarnings("unchecked")
@Controller(value = "/req")
public class RequestController {

    @Autowired
    UserService userService;

    @Autowired
    StockService stockService;

    @Autowired
    RequestDao requestDao;

    @RequestMapping(method = RequestMethod.GET, value = "/welcome")
    public String showWelcomePage() {
        return "welcome";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/error")
    public String showErrorPage(HttpServletRequest request, ModelMap model) {
        int errorType = ServletRequestUtils.getIntParameter(request, "type", 0);
        String msg = StockUtils.getErrorMsg(errorType);
        model.put("msg", msg);
        return "error";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/welcome")
    public String postRequest(HttpServletRequest request) {
        String gmailAddress = ServletRequestUtils.getStringParameter(request, "gmailAddress", null);

        if (requestDao.getUserAccess(gmailAddress) != null) {
            return redirect(StockUtils.ERROR_TYPE_ALREADY_EXISTS);
        }
        requestDao.save(new UserAccess(gmailAddress, new Date(), false));

        stockService.sendMail(new String[]{StockUtils.ADMIN_EMAIL},
                "DSEBD Subscription ALERT!",
                gmailAddress + " has recently subscribed to DSE ALERT!");
        return "done";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/pending")
    public String showPedingRequests(ModelMap model) {
        if (!userService.isUserAdmin()) {
            return redirect(StockUtils.ERROR_TYPE_UNAUTHORIZED);
        }
        Collection<UserAccess> pendingList = requestDao.getPendingRequests();
        model.put("type", "pending");
        model.put("list", pendingList);
        return "requests";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/pending")
    public String allowAccess(HttpServletRequest request) {
        if (!userService.isUserAdmin()) {
            return redirect(StockUtils.ERROR_TYPE_UNAUTHORIZED);
        }
        String[] emails = ServletRequestUtils.getStringParameters(request, "emails");
        requestDao.allowAccess(Arrays.asList(emails));

        stockService.sendMail(emails,
                "DSE ALERT! :: Accepted",
                "Your request has been accepted. Please visit http://dse-alert.appspot.com to use the service.");

        return "redirect:/req/pending";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/approved")
    public String showApprovedRequests(ModelMap model) {
        if (!userService.isUserAdmin()) {
            return redirect(StockUtils.ERROR_TYPE_UNAUTHORIZED);
        }
        Collection<UserAccess> approvedList = requestDao.getApprovedRequests();
        model.put("type", "approved");
        model.put("list", approvedList);
        return "requests";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/approved")
    public String denyAccess(HttpServletRequest request) {
        if (!userService.isUserAdmin()) {
            return redirect(StockUtils.ERROR_TYPE_UNAUTHORIZED);
        }
        String[] emails = ServletRequestUtils.getStringParameters(request, "emails");
        requestDao.denyAccess(Arrays.asList(emails));
        return "redirect:/req/approved";
    }

    private String redirect(int errorType) {
        return "redirect:" + StockUtils.ERROR_PAGE + "?type=" + errorType;
    }
}
